#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution 
{
public:
    TreeNode* searchBST(TreeNode* root, int val) 
    {
        if(root == nullptr)
        {
            return nullptr;
        }
        stack<pair<TreeNode * , bool>> st;
        st.push(make_pair(root, false));
        while(!st.empty())
        {
            TreeNode * node = st.top().first;
            if(node->val == val)
            {
                return node;
            }
            bool visited = st.top().second;
            st.pop();
            if(visited)
            {
                continue;
            }
            st.push(make_pair(node, true));
            if(node->left != nullptr)
            {
                st.push(make_pair(node->left, false));
            }
            if(node->right != nullptr)
            {
                st.push(make_pair(node->right, false));
            }
        }
        return nullptr;
    }
};

// 辅助函数：从数组构建二叉搜索树（层序遍历方式，-1表示null）
TreeNode* buildTreeFromArray(vector<int>& arr) {
    if (arr.empty() || arr[0] == -1) return nullptr;
    
    TreeNode* root = new TreeNode(arr[0]);
    queue<TreeNode*> q;
    q.push(root);
    
    int i = 1;
    while (!q.empty() && i < arr.size()) {
        TreeNode* node = q.front();
        q.pop();
        
        // 左子节点
        if (i < arr.size() && arr[i] != -1) {
            node->left = new TreeNode(arr[i]);
            q.push(node->left);
        }
        i++;
        
        // 右子节点
        if (i < arr.size() && arr[i] != -1) {
            node->right = new TreeNode(arr[i]);
            q.push(node->right);
        }
        i++;
    }
    
    return root;
}

// 辅助函数：层序遍历打印二叉树
void printTree(TreeNode* root) {
    if (!root) {
        cout << "[]" << endl;
        return;
    }
    
    queue<TreeNode*> q;
    q.push(root);
    vector<string> result;
    
    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();
        
        if (node) {
            result.push_back(to_string(node->val));
            q.push(node->left);
            q.push(node->right);
        } else {
            result.push_back("null");
        }
    }
    
    // 移除末尾的null
    while (!result.empty() && result.back() == "null") {
        result.pop_back();
    }
    
    cout << "[";
    for (int i = 0; i < result.size(); i++) {
        cout << result[i];
        if (i < result.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

// 辅助函数：释放二叉树内存
void deleteTree(TreeNode* root) {
    if (!root) return;
    deleteTree(root->left);
    deleteTree(root->right);
    delete root;
}

int main() {
    Solution solution;
    
    cout << "=== LeetCode 700. 二叉搜索树中的搜索 测试案例 ===" << endl << endl;
    
    // 测试案例1：题目示例1
    cout << "测试案例1：" << endl;
    vector<int> tree1 = {4, 2, 7, 1, 3};
    TreeNode* root1 = buildTreeFromArray(tree1);
    int val1 = 2;
    
    cout << "输入: root = ";
    printTree(root1);
    cout << "      val = " << val1 << endl;
    
    TreeNode* result1 = solution.searchBST(root1, val1);
    cout << "输出: ";
    printTree(result1);
    cout << "期望: [2,1,3]" << endl << endl;
    
    // 测试案例2：题目示例2
    cout << "测试案例2：" << endl;
    vector<int> tree2 = {4, 2, 7, 1, 3};
    TreeNode* root2 = buildTreeFromArray(tree2);
    int val2 = 5;
    
    cout << "输入: root = ";
    printTree(root2);
    cout << "      val = " << val2 << endl;
    
    TreeNode* result2 = solution.searchBST(root2, val2);
    cout << "输出: ";
    printTree(result2);
    cout << "期望: []" << endl << endl;
    
    // 测试案例3：搜索根节点
    cout << "测试案例3（搜索根节点）：" << endl;
    vector<int> tree3 = {4, 2, 7, 1, 3};
    TreeNode* root3 = buildTreeFromArray(tree3);
    int val3 = 4;
    
    cout << "输入: root = ";
    printTree(root3);
    cout << "      val = " << val3 << endl;
    
    TreeNode* result3 = solution.searchBST(root3, val3);
    cout << "输出: ";
    printTree(result3);
    cout << "期望: [4,2,7,1,3]" << endl << endl;
    
    // 测试案例4：搜索叶子节点
    cout << "测试案例4（搜索叶子节点）：" << endl;
    vector<int> tree4 = {4, 2, 7, 1, 3};
    TreeNode* root4 = buildTreeFromArray(tree4);
    int val4 = 1;
    
    cout << "输入: root = ";
    printTree(root4);
    cout << "      val = " << val4 << endl;
    
    TreeNode* result4 = solution.searchBST(root4, val4);
    cout << "输出: ";
    printTree(result4);
    cout << "期望: [1]" << endl << endl;
    
    // 测试案例5：空树
    cout << "测试案例5（空树）：" << endl;
    TreeNode* root5 = nullptr;
    int val5 = 1;
    
    cout << "输入: root = ";
    printTree(root5);
    cout << "      val = " << val5 << endl;
    
    TreeNode* result5 = solution.searchBST(root5, val5);
    cout << "输出: ";
    printTree(result5);
    cout << "期望: []" << endl << endl;
    
    // 测试案例6：单节点树
    cout << "测试案例6（单节点树）：" << endl;
    vector<int> tree6 = {1};
    TreeNode* root6 = buildTreeFromArray(tree6);
    int val6 = 1;
    
    cout << "输入: root = ";
    printTree(root6);
    cout << "      val = " << val6 << endl;
    
    TreeNode* result6 = solution.searchBST(root6, val6);
    cout << "输出: ";
    printTree(result6);
    cout << "期望: [1]" << endl << endl;
    
    // 清理内存
    deleteTree(root1);
    deleteTree(root2);
    deleteTree(root3);
    deleteTree(root4);
    deleteTree(root6);
    
    cout << "所有测试案例执行完毕！" << endl;
    
    return 0;
}